package com.yoyo.ad.yoyo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;

import com.dollkey.hdownload.serv.DownLoadNormalService;
import com.dollkey.hdownload.util.ValidateUtil;
import com.yoyo.ad.bean.YoYoAdBean;
import com.yoyo.ad.main.AdModelType;
import com.yoyo.ad.main.IAdClick;
import com.yoyo.ad.main.YoYoAd;
import com.yoyo.ad.utils.ActivityUtils;
import com.yoyo.ad.utils.StringUtil;

import static com.yoyo.ad.confusion.AdManage.YOYO;


class YYAd implements YoYoAd {
    private static final String TAG = "YYAd";
    private final int TYPE_DOWNLOAD = 1;
    private final int TYPE_HTML = 2;

    private IAdClick mIAdClick;

    YYAd(YoYoAdBean yoYoAdBean, IAdClick adClick) {
        this.mYoYoAdBean = yoYoAdBean;
        this.mIAdClick = adClick;
    }

    private YoYoAdBean mYoYoAdBean;
    private ViewGroup mViewGroup;

    @Override
    public int getSource() {
        return YOYO;
    }

    @Override
    public String getTitle() {
        return null;
    }

    @Override
    public String getDescription() {
        return null;
    }

    @Override
    public Bitmap getLogo() {
        return null;
    }

    @Override
    public String getIconUrl() {
        return null;
    }

    @Override
    public String getImgUrl1() {
        if (mYoYoAdBean != null) {
            return StringUtil.notNull(mYoYoAdBean.getResp_data().getActiveBannerConfig().getActiveImgUrl());
        }
        return null;
    }

    @Override
    public String getImgUrl2() {
        return null;
    }

    @Override
    public String getImgUrl3() {
        return null;
    }

    @Override
    public String getBrandName() {
        return "yoyo";
    }

    public int getModel() {
        return AdModelType.MODEL_UNKNOW;
    }

    @Override
    public View getView() {
        return null;
    }

    @Override
    public boolean isNativeExpress() {
        return false;
    }

    @Override
    public void exposure(View view) {

    }

    @Override
    public void onAdClicked(final View view, final ViewGroup viewGroup) {
        if (viewGroup != null)
            viewGroup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    handleClick(v);
                }
            });
        else
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    handleClick(v);
                }
            });
    }

    private void handleClick(View v) {
        if (mYoYoAdBean != null) {
            Context context = v.getContext();
            String toappUrl = mYoYoAdBean.getResp_data().getActiveBannerConfig().getToappUrl();
            if (mYoYoAdBean.getResp_data().getActiveBannerConfig().getToappType() == TYPE_DOWNLOAD) {
                String packageName = mYoYoAdBean.getResp_data().getActiveBannerConfig().getPkgname();

                if (ActivityUtils.isAppInstalled(context, packageName)) {
                    Intent intent = ActivityUtils.getAppOpenIntentByPackageName(context, packageName);
                    if (intent == null) {
                        context.startService(new Intent(context, DownLoadNormalService.class)
                                .putExtra(DownLoadNormalService.PARAM_APK_URL, toappUrl)
                                .putExtra(DownLoadNormalService.PARAM_APK_NAME, packageName));
                    } else {
                        context.startActivity(intent);
                    }
                } else {
                    context.startService(new Intent(context, DownLoadNormalService.class)
                            .putExtra(DownLoadNormalService.PARAM_APK_URL, toappUrl)
                            .putExtra(DownLoadNormalService.PARAM_APK_NAME, packageName));
                }
            } else {
//                AdWebViewActivity.newInstance(context, toappUrl);
                if (!ValidateUtil.isUrl(toappUrl)) {
                    toappUrl = "http://".concat(toappUrl);
                }
                Uri uri = Uri.parse(toappUrl);
//                Uri uri = Uri.parse("https://www.baidu.com");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        }
        if (mIAdClick != null) {
            mIAdClick.onAdClick(v);
        }
    }

    void load(ViewGroup viewGroup) {
        mViewGroup = viewGroup;
    }

    @Override
    public void show(Activity activity) {
        if (mViewGroup != null) {
            mViewGroup.setVisibility(View.VISIBLE);
            ViewGroup contentView = (ViewGroup) activity.getWindow().getDecorView();
            contentView.addView(mViewGroup, new ViewGroup.LayoutParams(-1, -1));
        }
        if (mIAdClick != null) {
            mIAdClick.onAdShow();
        }
    }

    @Override
    public void release() {

    }
}
